/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sdsmdg.harjot.vectormasterdemo;

import com.sdsmdg.harjot.vectormaster.VectorMasterDrawable;
import com.sdsmdg.harjot.vectormaster.VectorMasterView;
import com.sdsmdg.harjot.vectormaster.models.GroupModel;
import com.sdsmdg.harjot.vectormaster.models.PathModel;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.utils.Color;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Main Ability class
 */
public class MainAbility extends Ability {
    private VectorMasterView lightningView;
    private VectorMasterView hourglassView;
    private VectorMasterView searchBackView;
    private VectorMasterView rainView;
    private VectorMasterView heart;
    private VectorMasterView heart1;
    private VectorMasterView heart2;
    private VectorMasterView heart3;
    private VectorMasterView heart4;
    private VectorMasterView heart5;

    private float trimStart = 0;
    private float trimEnd = 0;
    private int countLightning = 60;
    private float heartTrimEnd = 0;
    private int heartC = 60;
    private float heartAlpha = 0;
    private int heartF = 60;
    private float translationY = 0;
    private float rotation = 0;
    private int state = 0;
    private float stemTrimStart = 0;
    private float stemTrimEnd = 0.185f;
    private float circleTrimEnd = 1;
    private float arrowHeadTopTrimEnd = 0;
    private float arrowHeadBottomTrimEnd = 0;
    private int searchBackState = 0;
    private int count = 60;
    private float rainTrimStart_C = 0;
    private float rainTrimEnd_C = 0;
    private int rainCount_C = 60;
    private float rainTrimStart_L = 0;
    private float rainTrimEnd_L = 0;
    private int rainCount_L = 55;
    private float rainTrimStart_R = 0;
    private float rainTrimEnd_R = 0;
    private int rainCount_R = 50;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

//        VectorMasterDrawable masterDrawable = new VectorMasterDrawable(this, ResourceTable.Graphic_ic_heart);

        lightningView = (VectorMasterView) findComponentById(ResourceTable.Id_vector_master);
        animateLightning();

        hourglassView = (VectorMasterView) findComponentById(ResourceTable.Id_vector_master_1);
        animateHourglass();

        searchBackView = (VectorMasterView) findComponentById(ResourceTable.Id_vector_master_2);
        animateSearchBack();

        rainView = (VectorMasterView) findComponentById(ResourceTable.Id_vector_master_3);
        animateRain();

        heart = (VectorMasterView) findComponentById(ResourceTable.Id_vector_master_heart);
        // find the correct path using name
        PathModel outline = heart.getPathModelByName("outline");
        // set the stroke color
        outline.setStrokeColor(Color.getIntColor("#FFD700"));

        heart1 = (VectorMasterView) findComponentById(ResourceTable.Id_vector_master_heart_1);
        PathModel outline1 = heart1.getPathModelByName("outline");
        // set the fill color (if fill color is not set or is TRANSPARENT, then no fill is drawn)
        outline1.setFillColor(Color.getIntColor("#FFD700"));
        outline1.setStrokeColor(Color.DKGRAY.getValue());

        heart2 = (VectorMasterView) findComponentById(ResourceTable.Id_vector_master_heart_2);
        PathModel outline2 = heart2.getPathModelByName("outline");
        // set the fill color (if fill color is not set or is TRANSPARENT, then no fill is drawn)
        outline2.setFillColor(Color.DKGRAY.getValue());
        outline2.setStrokeColor(Color.DKGRAY.getValue());
        // set trim path start (values are given in fraction of length)
        outline2.setTrimPathStart(0.0f);
        // set trim path end (values are given in fraction of length)
        outline2.setTrimPathEnd(0.65f);

        heart3 = (VectorMasterView) findComponentById(ResourceTable.Id_vector_master_heart_3);
        PathModel outline3 = heart3.getPathModelByName("outline");
        // set the fill color (if fill color is not set or is TRANSPARENT, then no fill is drawn)
        outline3.setFillColor(Color.getIntColor("#FFD700"));
        outline3.setStrokeColor(Color.getIntColor("#FFD700"));
        // set trim path start (values are given in fraction of length)
        outline3.setTrimPathStart(0.17f);
        // set trim path end (values are given in fraction of length)
        outline3.setTrimPathEnd(0.65f);

        heart4 = (VectorMasterView) findComponentById(ResourceTable.Id_vector_master_heart_4);
        animateHeart();
        heart5 = (VectorMasterView) findComponentById(ResourceTable.Id_vector_master_heart_5);
        animateHeart1();
    }

    private void animateHeart() {
        PathModel model = heart4.getPathModelByName("outline");
        model.setStrokeColor(Color.getIntColor("#000000"));
        model.setTrimPathEnd(0.0f);
        model.setTrimPathStart(0.0f);
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                heartC++;
                if (heartC >= 60) {
                    if (heartTrimEnd < 1) {
                        heartTrimEnd += 0.04f;
                    } else if (heartTrimEnd >= 1 && trimStart < 1) {
                        trimStart += 0.04f;
                    } else if (heartTrimEnd >= 1 && trimStart >= 1) {
                        heartTrimEnd = 0;
                        trimStart = 0;
                        heartC = 0;
                    }
                    model.setTrimPathEnd(heartTrimEnd);
                    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
                    eventHandler.postTask(new Runnable() {
                        @Override
                        public void run() {
                            heart4.update();
                        }
                    });
                }
            }
        }, 500, 1000 / 60);
    }

    private void animateHeart1() {
        PathModel model = heart5.getPathModelByName("outline");
        model.setStrokeColor(Color.getIntColor("#FFD700"));
        model.setFillColor(Color.getIntColor("#FFD700"));
        model.setFillAlpha(0.0f);

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                heartF++;
                if (heartF >= 60) {
                    if (heartAlpha < 1) {
                        heartAlpha += 0.04f;
                    } else {
                        heartAlpha = 0;
                        heartF = 0;
                    }
                    model.setFillAlpha(heartAlpha);
                    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
                    eventHandler.postTask(new Runnable() {
                        @Override
                        public void run() {
                            heart5.update();
                        }
                    });
                }
            }
        }, 500, 1000 / 60);
    }

    private void animateLightning() {
        final PathModel lightningModel = lightningView.getPathModelByName("lightning");
        lightningModel.setStrokeColor(Color.getIntColor("#FFD700"));
        lightningModel.setTrimPathEnd(0.0f);
        lightningModel.setTrimPathStart(0.0f);

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                countLightning++;
                if (countLightning >= 60) {
                    if (trimEnd < 1) {
                        trimEnd += 0.04f;
                    } else if (trimEnd >= 1 && trimStart < 1) {
                        trimStart += 0.04f;
                    } else if (trimEnd >= 1 && trimStart >= 1) {
                        trimEnd = 0;
                        trimStart = 0;
                        countLightning = 0;
                    }
                    lightningModel.setTrimPathEnd(trimEnd);
                    lightningModel.setTrimPathStart(trimStart);
                    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
                    eventHandler.postTask(new Runnable() {
                        @Override
                        public void run() {
                            lightningView.update();
                        }
                    });
                }
            }
        }, 500, 1000 / 60);
    }


    private void animateHourglass() {
        final GroupModel frame = hourglassView.getGroupModelByName("hourglass_frame");
        final GroupModel fillOutlines = hourglassView.getGroupModelByName("fill_outlines");
        final GroupModel fillOutlinesPivot = hourglassView.getGroupModelByName("fill_outlines_pivot");
        final GroupModel groupFillPath = hourglassView.getGroupModelByName("group_fill_path");

        state = 0;
        translationY = -24;

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (state == 0) {
                    translationY += 0.3f;
                    fillOutlinesPivot.setTranslateY(translationY);
                    groupFillPath.setTranslateY(-1 * translationY);
                    if (translationY >= -12) {
                        state = 1;
                    }
                } else if (state == 1) {
                    rotation += 3f;
                    frame.setRotation(rotation);
                    fillOutlines.setRotation(rotation);
                    if (rotation == 180) {
                        state = 2;
                    }
                } else if (state == 2) {
                    translationY -= 0.3f;
                    fillOutlinesPivot.setTranslateY(translationY);
                    groupFillPath.setTranslateY(-1 * translationY);
                    if (translationY <= -24) {
                        state = 3;
                    }
                } else if (state == 3) {
                    rotation += 3f;
                    frame.setRotation(rotation);
                    fillOutlines.setRotation(rotation);
                    if (rotation == 360) {
                        rotation = 0;
                        state = 0;
                    }
                }
                EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
                eventHandler.postTask(new Runnable() {
                    @Override
                    public void run() {
                        hourglassView.update();
                    }
                });
            }
        }, 500, 1000 / 60);
    }

    private void animateSearchBack() {
        final PathModel searchCircle = searchBackView.getPathModelByName("search_circle");
        final PathModel stem = searchBackView.getPathModelByName("stem");
        final PathModel arrowUp = searchBackView.getPathModelByName("arrow_head_top");
        final PathModel arrowDown = searchBackView.getPathModelByName("arrow_head_bottom");
        searchCircle.setStrokeColor(Color.getIntColor("#FFD700"));
        stem.setStrokeColor(Color.getIntColor("#FFD700"));
        arrowUp.setStrokeColor(Color.getIntColor("#FFD700"));
        arrowDown.setStrokeColor(Color.getIntColor("#FFD700"));
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                count++;
                if (count >= 50) {
                    if (searchBackState == 0) {
                        circleTrimEnd -= 1.0f / 20;
                        stemTrimStart += 0.75f / 20;
                        stemTrimEnd += (1 - 0.185f) / 20;
                        arrowHeadBottomTrimEnd += 1.0f / 20;
                        arrowHeadTopTrimEnd += 1.0f / 20;
                        if (circleTrimEnd <= 0) {
                            searchBackState = 1;
                            count = 0;
                        }
                    } else if (searchBackState == 1) {
                        arrowHeadBottomTrimEnd -= 1.0f / 20;
                        arrowHeadTopTrimEnd -= 1.0f / 20;
                        stemTrimStart -= 0.75f / 20;
                        stemTrimEnd -= (1 - 0.185f) / 20;
                        circleTrimEnd += 1.0f / 20;
                        if (circleTrimEnd >= 1) {
                            searchBackState = 0;
                            count = 0;
                        }
                    }
                    searchCircle.setTrimPathEnd(circleTrimEnd);
                    stem.setTrimPathEnd(stemTrimEnd);
                    stem.setTrimPathStart(stemTrimStart);
                    arrowUp.setTrimPathEnd(arrowHeadTopTrimEnd);
                    arrowDown.setTrimPathEnd(arrowHeadBottomTrimEnd);
                    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
                    eventHandler.postTask(new Runnable() {
                        @Override
                        public void run() {
                            searchBackView.update();
                        }
                    });
                }
            }
        }, 1000, 1000 / 60);
    }

    private void animateRain() {
        final PathModel rainLeft = rainView.getPathModelByName("rain_left");
        final PathModel rainCenter = rainView.getPathModelByName("rain_center");
        final PathModel rainRight = rainView.getPathModelByName("rain_right");
        rainLeft.setStrokeColor(Color.getIntColor("#FFD700"));
        rainCenter.setStrokeColor(Color.getIntColor("#FFD700"));
        rainRight.setStrokeColor(Color.getIntColor("#FFD700"));

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                rainCount_C++;
                if (rainCount_C >= 60) {
                    if (rainTrimEnd_C < 1) {
                        rainTrimEnd_C += 0.04f;
                    } else if (rainTrimEnd_C >= 1 && rainTrimStart_C < 1) {
                        rainTrimStart_C += 0.04f;
                    } else if (rainTrimEnd_C >= 1 && rainTrimStart_C >= 1) {
                        rainTrimEnd_C = 0;
                        rainTrimStart_C = 0;
                        rainCount_C = 0;
                    }
                }
                rainCount_L++;
                if (rainCount_L >= 60) {
                    if (rainTrimEnd_L < 1) {
                        rainTrimEnd_L += 0.04f;
                    } else if (rainTrimEnd_L >= 1 && rainTrimStart_L < 1) {
                        rainTrimStart_L += 0.04f;
                    } else if (rainTrimEnd_L >= 1 && rainTrimStart_L >= 1) {
                        rainTrimEnd_L = 0;
                        rainTrimStart_L = 0;
                        rainCount_L = 0;
                    }
                }
                rainCount_R++;
                if (rainCount_R >= 60) {
                    if (rainTrimEnd_R < 1) {
                        rainTrimEnd_R += 0.04f;
                    } else if (rainTrimEnd_R >= 1 && rainTrimStart_R < 1) {
                        rainTrimStart_R += 0.04f;
                    } else if (rainTrimEnd_R >= 1 && rainTrimStart_R >= 1) {
                        rainTrimEnd_R = 0;
                        rainTrimStart_R = 0;
                        rainCount_R = 0;
                    }
                }

                rainCenter.setTrimPathEnd(rainTrimEnd_C);
                rainCenter.setTrimPathStart(rainTrimStart_C);

                rainLeft.setTrimPathEnd(rainTrimEnd_L);
                rainLeft.setTrimPathStart(rainTrimStart_L);

                rainRight.setTrimPathEnd(rainTrimEnd_R);
                rainRight.setTrimPathStart(rainTrimStart_R);
                EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
                eventHandler.postTask(new Runnable() {
                    @Override
                    public void run() {
                        rainView.update();
                    }
                });
            }
        }, 500, 1000 / 60);
    }
}
